package com.finalProject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * author: Jimmmy Smile
 * date: 2021-06-09 22:17::17
 * positioan: com.finalProject
 * e-mail:jimmysmileltl@163.com
 **/
public class DBManager_Income {
    private DBHelper_Income dbHelper;
    private String TBNAME;

    public DBManager_Income(Context context) {
        dbHelper = new DBHelper_Income(context);
        TBNAME = DBHelper_Income.TB_NAME;
    }

    public void add(Income item){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("rank", item.getOrder());
        values.put("city", item.getCity());
        values.put("income", item.getIncome());
        db.insert(TBNAME, null, values);
        db.close();
    }

    public void addAll(List<Income> list){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        for (Income item : list) {
            ContentValues values = new ContentValues();
            values.put("rank", item.getOrder());
            values.put("city", item.getCity());
            values.put("income", item.getIncome());
            db.insert(TBNAME, null, values);

        }
        Log.i("addaded", "add成功 ");
        db.close();
    }

    public void deleteAll(){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(TBNAME,null,null);
        db.close();
    }

    public void delete(int id){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(TBNAME, "ID=?", new String[]{String.valueOf(id)});
        db.close();
    }

    public void update(Income item){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("rank", item.getOrder());
        values.put("city", item.getCity());
        values.put("income", item.getIncome());
        
        db.update(TBNAME, values, "city=?", new String[]{String.valueOf(item.getCity())});
        db.close();
    }

    public List<Income> listAll(){
        List<Income> rateList = null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, null, null, null, null, null);
        if(cursor!=null){
            rateList = new ArrayList<Income>();
            while(cursor.moveToNext()){

                Income item = new Income();
                item.setOrder(cursor.getString(cursor.getColumnIndex("rank")));
                item.setCity(cursor.getString(cursor.getColumnIndex("city")));
                item.setIncome(cursor.getString(cursor.getColumnIndex("income")));

                rateList.add(item);
            }
            Log.i("listlist", "listAll:list成功 ");
            cursor.close();
        }
        else{
            Log.i("listall", "listAll: null");
        }
        db.close();
        return rateList;

    }

    public Income findById(int id){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, "ID=?", new String[]{String.valueOf(id)}, null, null, null);
        Income Income = null;
        if(cursor!=null && cursor.moveToFirst()){
            Income = new Income();
            Income.setOrder(cursor.getString(cursor.getColumnIndex("rank")));
            Income.setCity(cursor.getString(cursor.getColumnIndex("city")));
            Income.setIncome(cursor.getString(cursor.getColumnIndex("income")));

            cursor.close();
        }
        db.close();
        return Income;
    }
}
